<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019-10-18
 * Time: 11:06
 */

namespace app\manage\admin;

use app\admin\controller\Admin;
use app\common\builder\ZBuilder;

// 引入ZBuilder
use app\common\model\TaskType as TaskTypeModel;
use think\Db;

class TaskType extends Admin
{
    //新闻分类列表
    public function lists( )
    {
        // 读取数据
        $map = $this->getMap();
        // 数据列表
        $data_list = Db::name('qt_task_type')
            ->alias('ty')
            ->leftJoin('qt_post p','ty.post_id = p.id')
            ->field('ty.*,p.title')
            ->where($map)
            ->order('id desc')
            ->paginate();
        // 分页数据
        $page = $data_list->render();
        $btn_access1 = [
            'title' => '分类编辑',
            'icon' => 'fa fa-fw fa-pencil',
            'class' => 'btn btn-xs btn-default',
            'href' => url('edit', ['id' => '__id__']),
        ];
        $btn_access2 = [
            'title' => '删除分类',
            'icon' => 'fa fa-fw fa-trash-o',
            'class' => 'btn btn-xs btn-default ajax-get confirm',
            'href' => url('delete', ['id' => '__id__']),
        ];
        $btn_access3 = [
            'title' => '新增任务分类',
            'icon' => 'fa fa-fw fa-plus',
            'class' => 'btn btn-info',
            'href' => url('add'),
        ];
        $post_category = Db::name('qt_post')->order('id asc')->column('id,title');
        //设置页面
        return ZBuilder::make('table')
            ->hideCheckbox()
            ->addColumn('id', '编号', 'text')
            ->addColumn('name', '任务分类', 'text')
            ->addColumn('title', '所属岗位', 'text')
            ->addColumn('sort', '排序', 'text')
            ->addColumn('status', '状态', 'status', '', ['1' => '显示', '2' => '隐藏'])
            ->addColumn('created_time', '创建时间', 'datetime', '', 'Y-m-d H:i:s')
            ->addColumn('right_button', '操作', 'btn')
            ->setSearchArea([
                ['select:2', 'ty.post_id', '所属岗位', 'eq', '', $post_category],
                ['text:2', 'ty.name', '任务分类', 'like'],
            ])
            ->addTopButton('single', $btn_access3) // 添加顶部按钮
            ->addRightButton('edit', $btn_access1) // 添加右侧按钮
            ->addRightButton('delete', $btn_access2) // 添加右侧按钮
            ->setPrimaryKey('id') // 设置主键名为username
            ->setColumnWidth([
                'id'  => 30,
                'status' => 40,
                'right_button'=>50,
                'name'=>100,
                'title'=>60
            ])
            ->setPageTitle('任务分类')
            ->setRowList($data_list) // 设置表格数据
            ->setPages($page) // 设置分页数据
            ->fetch();
    }

    //添加日报审批分类
    public function add( )
    {
        //判断是否提交信息
        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();
            $result = $this->validate($data, 'DailyType.add');
            if (true !== $result) $this->error($result);
            $data['created_time'] = time();
            $data['pid'] = '0';
            if (TaskTypeModel::create($data)) {
                $this->success('新增成功', url('lists'));
            } else {
                $this->error('新增失败');
            }
        } else {
            $groupRes = Db::name('qt_group')->whereIn('pid', ['0','1'])->column('id,name');
            $post_category = Db::name('qt_post')->order('id asc')->column('id,title');
            return ZBuilder::make('form')
                ->addFormItems(
                    [
                        ['Linkage:7','team_id','所属团队','<code>请选择所属团队</code>',$groupRes,'',url('get_post'),'post_id'],
                        ['select:7','post_id','所属岗位'],
                        ['text:7', 'name', '分类名称'],
                        ['text:7', 'sort', '排序','','50'],
                        ['radio:', 'status', '状态', '', ['1' => '显示', '2' => '隐藏'], '1'],
                    ]
                )
                ->setPageTitle('新增一级分类')
                ->fetch();
        }
    }


    //编辑任务分类
    public function edit($id = null)
    {
        //判断是否提交信息
        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();
            // 验证
            $result = $this->validate($data, 'DailyType.add');
            if (true !== $result) $this->error($result);

            if (TaskTypeModel::update($data)) {
                $this->success('编辑成功', 'lists');
            } else {
                $this->error('编辑失败');
            }
        } else {
            $info = TaskTypeModel::get($id);
            $groupRes = Db::name('qt_group')->whereIn('pid', ['0','1'])->column('id,name');
            $post_category = Db::name('qt_post')->order('id asc')->column('id,title');
            return ZBuilder::make('form')
                ->addFormItems(
                    [
                        ['hidden', 'id', 'id'],
                        ['Linkage:7','team_id','所属团队','<code>请选择所属公司</code>',$groupRes,'',url('get_post'),'post_id'],
                        ['select:7','post_id','所属岗位','<code>请选择所属岗位</code>',$post_category],
                        ['text:7', 'name', '标题'],
                        ['text:7', 'sort', '排序','','50'],
                        ['radio', 'status', '状态', '', [1 => '显示', 2 => '隐藏']]
                    ]
                )
                ->setFormData($info)
                ->setPageTitle('编辑分类')
                ->fetch();
        }
    }

    //删除任务分类
    public function delete($id = null)
    {
        if ($id === null) $this->error('缺少参数');
        $son = Db::name('qt_task')->where('task_id', $id)->count();
        if ($son > 0) {
            $this->error('该任务分类下面有任务，不能删除', 'lists');
        }
        $re = TaskTypeModel::destroy($id);// 删除主键为1,2,3的数据
        if ($re) {
            $this->success('删除成功', 'lists');
        } else {
            $this->error('系统繁忙请稍后再试', 'lists');
        }

    }


    /**
     * 获取当前团队下面的岗位
     * @param $team_id
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function get_post($team_id){
        $arr['code'] = '1'; //判断状态
        $arr['msg'] = '请求成功'; //回传信息
        $data= Db::name('qt_post')->where('team_id',$team_id)->field('id,title')->select(); //数据
        $result = [];
        foreach ($data as $key=>$value){
            $result[$key]['key'] = $value['id'];
            $result[$key]['value'] = $value['title'];
        }
        $arr['list'] = $result;
        return json($arr);
    }


}